package com.example.lovewordsapplication.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.lovewordsapplication.R;
import com.example.lovewordsapplication.entity.Words;

import java.util.List;

/**
 * @author Leung
 * @date 2020/4/7 23:37
 */
public class WordsAdapter extends RecyclerView.Adapter<WordsAdapter.ViewHolder> {

    private List<Words.NewslistBean> list;
    private View.OnLongClickListener txtOnLongClickListener;

    public WordsAdapter(List<Words.NewslistBean> list) {
        this.list = list;
    }

    public void setTxtOnLongClickListener(View.OnLongClickListener txtOnLongClickListener) {
        this.txtOnLongClickListener = txtOnLongClickListener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(
                R.layout.message_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Words.NewslistBean bean = list.get(position);
        String content = bean.getContent();
        if (!bean.isSendType()) {
            holder.llMsgLeftContainer.setVisibility(View.VISIBLE);
            holder.llMsgRightContainer.setVisibility(View.GONE);
            holder.tvMsgLeftTxt.setText(content);
            holder.tvMsgLeftTxt.setOnLongClickListener((v -> txtOnLongClickListener.onLongClick(v)));
        } else {
            holder.llMsgLeftContainer.setVisibility(View.GONE);
            holder.llMsgRightContainer.setVisibility(View.VISIBLE);
            holder.tvMsgRightTxt.setText(content);
            holder.tvMsgRightTxt.setOnLongClickListener((v -> txtOnLongClickListener.onLongClick(v)));
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        View rootView;
        TextView tvMsgLeftTxt;
        LinearLayout llMsgLeftContainer;
        TextView tvMsgRightTxt;
        LinearLayout llMsgRightContainer;

        ViewHolder(View rootView) {
            super(rootView);
            this.rootView = rootView;
            this.tvMsgLeftTxt = (TextView) rootView.findViewById(R.id.tv_msg_leftTxt);
            this.llMsgLeftContainer = (LinearLayout) rootView.findViewById(R.id.ll_msg_leftParent);
            this.tvMsgRightTxt = (TextView) rootView.findViewById(R.id.tv_msg_rightTxt);
            this.llMsgRightContainer = (LinearLayout) rootView.findViewById(R.id.ll_msg_rightParent);
        }

    }
}
